'use strict'
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const path = require('path')
const defaultSettingS = require('./src/setting')
const name = defaultSettingS.title

function resolve (dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  publicPath: './',
  // outputDir: 'dev',
  assetsDir: 'static',
  indexPath: 'index.html',
  productionSourceMap: false,
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack: config => {
    config.plugins.delete('prefetch')
    config.module.rule('svg').exclude.add(resolve('src/icons')).end()
    config.module.rule('icons').test(/\.svg$/).include.add(resolve('src/icons')).end().use('svg-sprite-loader').loader('svg-sprite-loader').options({ symbolId: 'icon-[name]' }).end()
    config.when(process.env.NODE_ENV !== 'development', con => {
      con.plugin('ScriptExtHtmlWebpackPlugin').after('html').use('script-ext-html-webpack-plugin', [{ inline: /runtime\..*\.js$/ }]).end()
      con.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          }
        }
      })
      con.optimization.runtimeChunk('single')
      con.plugin('compressionPlugin').use(
        new CompressionWebpackPlugin({
          algorithm: 'gzip',
          test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
          threshold: 10240,
          minRatio: 0.8
        })
      )
    })
  },
  devServer: {
    open: false,
    host: '0.0.0.0',
    port: 8080,
    https: false,
    hot: true,
    hotOnly: false,
    overlay: {
      warnings: true,
      errors: true
    },
    proxy: {
      '/bigdata/risk': {
        target: 'http://dev-p0408-b007-bigdata-risk-platform:30080',
        changeOrigin: true,
        pathRewrite: {
          '^/bigdata/risk': '/bigdata/risk'
        }
      }
    }
  }
}
